#!/usr/bin/env python
#coding:utf8

from random import randrange

'''
作业: 定义一个函数参数为接收一个无序的列表打印出这个列表的最大值和最小值. 不建议使用sort方法
方法:
    1、用递归可以
    2、冒泡算法, 相邻两个对象比较大小交换位置。从小到大排列, 取下标0和下-1位置的元素.
    3、list的内置sort或内建sorted函数
'''

'''
随机生成一个列表
>>> [randrange(100) for _ in xrange(20)]
[87, 16, 98, 60, 14, 99, 45, 50, 58, 34, 14, 49, 48, 67, 91, 15, 18, 99, 77, 98]

>>> [randrange(100) for _ in xrange(20)]
[72, 54, 80, 36, 57, 39, 1, 31, 37, 72, 63, 65, 34, 83, 22, 56, 14, 43, 95, 56]

>>> [randrange(100) for _ in xrange(20)]
[24, 89, 54, 53, 60, 38, 54, 70, 77, 33, 72, 36, 16, 33, 24, 13, 93, 91, 48, 72]
'''

def random_list(number=20):
    '''docstring for the function'''
    return [randrange(100) for _ in xrange(number)]


def min_max_v1():
    '''docstring for the function'''
    items = [randrange(100) for _ in xrange(20)]

    for m in xrange(1, len(items)):
        for n in xrange(len(items)-m):
            if items[n] > items[n+1]:
                items[n], items[n+1] = items[n+1], items[n]
    ret = min, max = items[0], items[-1]
    # is python3
    # ret = min, *args, max = lists
    # return (min, max)
    return ret

def min_max_v2(lists):
    '''docstring for the function'''
    if not isinstance(lists, list):
        raise TypeError('must be an list object')

    for m in xrange(1, len(lists)):
        for n in xrange(len(lists)-m):
            if lists[n] > lists[n+1]:
                lists[n], lists[n+1] = lists[n+1], lists[n]
    ret = min, max = lists[0], lists[-1]
    # is python3
    # ret = min, *args, max = lists
    # return (min, max)
    return ret

if __name__ == '__main__':
    print(min_max_v1())
    print(min_max_v2(lists=random_list()))

'''
小结:
    冒泡, 相邻两个对象进行比较交换位置
    任何的对象都可以比较大小
    数字按值比较大小
    字符串按ord()后的结果比较大小
'''